//
//  ActivityDetailsViewController.m
//  newDouban
//
//  Created by dllo on 15/10/10.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ActivityDetailsViewController.h"
#import "ActivityDetailView.h"
#import "LoginViewController.h"
#import "MyActivityViewController.h"
#import "DataBaseSingTon.h"
#import "ActivityStore.h"
@interface ActivityDetailsViewController ()

@end

@implementation ActivityDetailsViewController

- (void)dealloc
{
    [_activity release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //self.navigationItem.title = @"罗马与巴洛克艺术";
    ActivityDetailView *activityDTV = [[ActivityDetailView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:activityDTV];
    [activityDTV release];
    
    activityDTV.activity = self.activity;
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)] autorelease];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)] autorelease];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    //NSString *sql = @"DROP TABLE activityStore";
    //[[DataBaseSingTon shareDataBase] dropTableAC:@"DROP TABLE activityStore"];
    [[DataBaseSingTon shareDataBase] openDBAC];
    [[DataBaseSingTon shareDataBase] createActivityStoreTable];
    
}


- (void)UIAlertController:(NSString *)str
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alertC animated:YES completion:^{
        
    }];
       [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

- (void)leftBarButtonAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)userDefault:(NSString *)str
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:str forKey:str];
    
    [userDefaults synchronize];
}


- (void)rightBarButtonAction:(UIBarButtonItem *)sender
{
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName1"];

    if (isLogin.length > 0) {
        
        //NSLog(@"self.activity.title %@", self.activity.title);

        NSMutableArray *arrM = [[DataBaseSingTon shareDataBase] selectActivityStoreSQL:[NSString stringWithFormat:@"SELECT * FROM activityStore WHERE userName = '%@' AND title = '%@'", isLogin, self.activity.title]];
        if (arrM.count == 0) {
            NSString *sql = [NSString stringWithFormat:@"INSERT INTO activityStore(title, userName) VALUES('%@', '%@')", self.activity.title, isLogin];
            [[DataBaseSingTon shareDataBase] insertActivitysql:sql];
            
            [self UIAlertController:@"收藏成功"];
        } else {
            [self UIAlertController:@"已经收藏"];
        }
       
        
    } else {
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *loginNC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNC animated:YES completion:^{
            
        }];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
